package club.hicode.daydayup.lambda;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.File;
import java.sql.SQLOutput;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 对于文件的 Lambda 测试
 *
 * @author Liu Chunfu
 * @date 2018-04-25 上午9:42
 **/
@Slf4j
public class FileLambdaTest1 {

    @Test
    public void test1() {
        File[] ls = FileUtil.ls("/Users/liuchunfu/Develop/temp/kj-dir/error");
        List<File> collect = Arrays.stream(ls).collect(Collectors.toList());
        System.out.println(collect.size());
    }

    @Test
    public void test2() {
        String[] args = new String[]{"1", "3", null};

        //System.out.println(args.length);
        //此处的 filter 到底什么意思？
        //List<String> collect = Arrays.stream(args).filter(a -> a == null).collect(Collectors.toList());
        Arrays.stream(args).filter(a -> a == null).collect(Collectors.toList()).forEach(System.out::println);
    }

    @Test
    public void test3() {
        List<Integer> integers = Arrays.asList(1, 22, 34);
        List<Integer> collect = integers.stream().filter(integer -> integer > 3).collect(Collectors.toList());
        //System.out.println(collect);

        Map<Integer, List<Integer>> collect1 = integers.stream().collect(Collectors.groupingBy(i -> i % 2));
        System.out.println(JSONUtil.toJsonPrettyStr(collect1));

    }

    @Test
    public void test() {
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < 10000; i++) {
            //map.put()
        }
    }

    @Test
    public void lambda() {
        long s0 = System.currentTimeMillis();
        DeviceBO bo = new DeviceBO();
        bo.setDeviceId("test");
        bo.setTimeSpan("test");
        bo.setPropertyMap(
                new HashMap<String, Double>() {
                    {
                        for (int i = 0; i < 1000000000; i++) {
                            put("k" + 0, Double.valueOf(i));
                        }
                    }
                });
        log.info("入库用时: " + (System.currentTimeMillis() - s0));


        //pre
        List<DevicePropertyBO> propertyBOList11 = new ArrayList<>();
        for (Map.Entry<String, Double> entry : bo.getPropertyMap().entrySet()) {
            DevicePropertyBO property = new DevicePropertyBO();
            property.setDeviceId(bo.getDeviceId());
            property.setTimeSpan(bo.getTimeSpan());
            property.setPropertyName(entry.getKey());
            property.setPropertyValue(entry.getValue());
            propertyBOList11.add(property);
        }
        List<DevicePropertyBO> propertyBOList22 = new ArrayList<>();
        bo.getPropertyMap().forEach((k, v) -> {
            DevicePropertyBO property = new DevicePropertyBO();
            property.setDeviceId(bo.getDeviceId());
            property.setTimeSpan(bo.getTimeSpan());
            property.setPropertyName(k);
            property.setPropertyValue(v);
            propertyBOList22.add(property);
        });

        List<DevicePropertyBO> propertyBOList1 = new ArrayList<>();
        List<DevicePropertyBO> propertyBOList = new ArrayList<>();

        long s2 = System.currentTimeMillis();

        for (Map.Entry<String, Double> entry : bo.getPropertyMap().entrySet()) {
            DevicePropertyBO property = new DevicePropertyBO();
            property.setDeviceId(bo.getDeviceId());
            property.setTimeSpan(bo.getTimeSpan());
            property.setPropertyName(entry.getKey());
            property.setPropertyValue(entry.getValue());
            propertyBOList1.add(property);
        }
        log.info("普通用时  " + (System.currentTimeMillis() - s2));


        long s1 = System.currentTimeMillis();

        bo.getPropertyMap().entrySet().parallelStream().forEach(entry -> {
            DevicePropertyBO property = new DevicePropertyBO();
            property.setDeviceId(bo.getDeviceId());
            property.setTimeSpan(bo.getTimeSpan());
            property.setPropertyName(entry.getKey());
            property.setPropertyValue(entry.getValue());
            propertyBOList.add(property);
        });
        log.info("lambda 用时  " + (System.currentTimeMillis() - s1));

    }
}
